import * as faceapi from "face-api.js";

main()

const classes = ['amy', 'bernadette', 'howard', 'leonard', 'penny', 'raj', 'sheldon', 'stuart']


async function main(){
    // load model
    await faceapi.nets.ssdMobilenetv1.loadFromUri('http://127.0.0.1:8888/weights')
    await faceapi.nets.faceLandmark68Net.loadFromUri('http://127.0.0.1:8888/weights')
    await faceapi.nets.faceRecognitionNet.loadFromUri('http://127.0.0.1:8888/weights')

    const allImgUris = classes.map(className=>{
        return [...Array(5)].map((_,index) => getImageUri(className,index+1) )        
    }).reduce((arr,arr2)=>arr.concat(arr2))

    const ts = Date.now()
    const images = await Promise.all(allImgUris.map(async uri=>faceapi.fetchImage(uri)))

    const landmarksByFace = await Promise.all(images.map(async image=> faceapi.detectLandmarks(image) ))
    landmarksByFace.forEach((landmarks,i)=>{
        const canvas = faceapi.createCanvasFromMedia(images[i])
        const dom = document.getElementById('faceContainer')
        dom.appendChild(canvas)
        new faceapi.draw.DrawFaceLandmarks(landmarks).draw(canvas)
    })

    const time = document.getElementById('time')
    time.value = Date.now()-ts +'ms'

    closeLoading()
}

// 获取图片
function getImageUri(className,num){
    return 'http://127.0.0.1:8888/images/'+className+'/'+className+num+'.png'
}